package com.crm.services;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crm.model.jdao.ITimeline;
import com.crm.model.mdao.TimelineMapper;
import com.crm.model.pojo.Timeline;

/**
 * 时间线服务类
 * @author Menghui
 *
 */
@Service
public class TimeLineServices {
	@Autowired
	ITimeline timeDAO;//时间线dao
	@Autowired
	TimelineMapper tlMapper;//时间线mybatis
	
	/**
	 * 新增时间线
	 * @return
	 */
	public int insertTimeLine(Timeline timeline) {
		Timeline tl = timeDAO.save(timeline);
		if(tl==null) {
			return 0;
		}
		return 1;
	}
	
	/**
	 * 查询全部时间线
	 * @return
	 */
	public List<Map<String, String>> selectAllLine(Integer custid) {
		List<Map<String, String>> selectOneTimeline = tlMapper.selectOneTimeline(custid);
		if(selectOneTimeline!=null) {
			return selectOneTimeline;
		}
		return null;
	}
	
	/**
	 * 查询订单总额
	 * @param custid
	 * @return
	 */
	public Object selectOrder(Integer custid) {		
		BigDecimal selectOrder = tlMapper.selectOrder(custid);
		if (selectOrder!=null) {
			return selectOrder;
		}
		return null;
	}
	
	/**
	 * 查询退货总额
	 * @param custid
	 * @return
	 */
	public Object selectOut(Integer custid) {
		BigDecimal selectOut = tlMapper.selectOut(custid);
		if (selectOut!=null) {
			return selectOut;
		}
		return null;
	}
	
	/**
	 * 查询发货金额
	 * @param custid
	 * @return
	 */
	public Object selectRe(Integer custid) {
		BigDecimal selectRe = tlMapper.selectRe(custid);
		if (selectRe!=null) {
			return selectRe;
		}
		return null;
	}
	
	/**
	 * 查询计划回款金额
	 * @param custid
	 * @return
	 */
	public Object selectReturnPlan(Integer custid) {
		BigDecimal selectReturnPlan = tlMapper.selectReturnPlan(custid);
		if (selectReturnPlan!=null) {
			return selectReturnPlan;
		}
		return null;
	}
	
	/**
	 * 查询实际回款
	 * @param custid
	 * @return
	 */
	public Object selectReturnIntenat(Integer custid) {
		BigDecimal selectReturnInt = tlMapper.selectReturnInt(custid);
		if (selectReturnInt!=null) {
			return selectReturnInt;
		}
		return null;
	}
	
	/**
	 * 查询智能回款金额
	 * @param custid
	 * @return
	 */
	public Object selectReturnIi(Integer custid) {
		//订单总额
		BigDecimal selectOrder = tlMapper.selectOrder(custid);
		//实际回款金额
		BigDecimal selectReturnInt = tlMapper.selectReturnInt(custid);
		BigDecimal returnInt=new BigDecimal(0);
		if (selectOrder!=null && selectReturnInt!=null) {
			//智能回款金额
			returnInt = selectOrder.subtract(selectReturnInt);
		}
		return returnInt;
	}
	
	/**
	 * 预付余额
	 * @param custid
	 * @return
	 */
	public Object selectAdvance(Integer custid) {
		//计划回款金额
		BigDecimal selectReturnPlan = tlMapper.selectReturnPlan(custid);
		//实际回款金额
		BigDecimal selectReturnInt = tlMapper.selectReturnInt(custid);
		BigDecimal advance = new BigDecimal(0);
		if (selectReturnPlan!=null && selectReturnInt!=null) {
			//预付余额
			advance =selectReturnPlan.subtract(selectReturnInt);
		}
		return advance;
	}
}
